<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="工作总结">
        <el-input v-model="queryParams.summary" placeholder="工作总结" clearable style="width: 240px;" />
      </el-form-item>


      <el-form-item label="明日计划">
        <el-input v-model="queryParams.plan" placeholder="明日计划" clearable style="width: 240px;" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['media:dailyReports:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['media:dailyReports:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['media:dailyReports:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyReportsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width=100 />
      <el-table-column label="项目名称" align="center" prop="reportName" show-overflow-tooltip/>
      <el-table-column label="工作总结" align="center" prop="summary" show-overflow-tooltip/>
      <el-table-column label="是否存在问题" align="center" prop="hasOpenIssues" >
        <template #default="scope">
          {{ scope.row.hasOpenIssues == 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="date" />
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary"  @click="handleInfo(scope.row)" v-hasPermi="['media:dailyReports:list']">查看详情</el-button>
          <el-button link type="primary"  @click="handleUpdate(scope.row)" v-hasPermi="['media:dailyReports:edit']">修改</el-button>
          <el-button link type="primary"  @click="handleDelete(scope.row)" v-hasPermi="['media:dailyReports:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作报告对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dailyReportsRef" :model="form" :rules="rules" label-width="80px">
        
        <el-form-item label="项目名称" >
          <el-input v-model="form.reportName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作总结" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        
        <el-form-item label="日报日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择日报日期">
          </el-date-picker>
        </el-form-item>
       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 查看详情对话框 -->
    <el-dialog :title="title" v-model="infoOpen" width="500px" append-to-body>
      <el-descriptions  :column="1"   border label-width="100px">
        <el-descriptions-item  label="项目名称" >
          <p v-html=" form.reportName "></p>
        </el-descriptions-item>

        <el-descriptions-item  label="工作总结" >
          <p v-html=" form.summary "></p>
        </el-descriptions-item>

        
        <el-descriptions-item  label="日报日期" >{{ form.date }}</el-descriptions-item>
        
      </el-descriptions>
      
    </el-dialog>
  </div>
</template>

<style >
.text-container {
  white-space: pre-line; /* 让文本中的换行符生效 */
}
</style>

<script setup name="DailyReports">
import { listDailyReports, getDailyReports, delDailyReports, addDailyReports, updateDailyReports } from "@/api/media/dailyReports";

const { proxy } = getCurrentInstance();

const dailyReportsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const infoOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    summary: null,
    plan: null,
    date: null,
  },
  rules: {
    summary: [
      { required: true, message: "今日工作总结不能为空", trigger: "blur" }
    ],
    plan: [
      { required: true, message: "明日工作计划不能为空", trigger: "blur" }
    ],
    date: [
      { required: true, message: "日报对应的日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询工作报告列表 */
function getList() {
  loading.value = true;
  listDailyReports(queryParams.value).then(response => {
    dailyReportsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    reportName: null,
    summary: null,
    plan: null,
    date: null,
    createdTime: null,
    updatedTime: null
  };
  proxy.resetForm("dailyReportsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {};
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加工作报告";
}

/** 查看详情按钮操作 */
function handleInfo(row) {
  reset();
  const _id = row.id 
  getDailyReports(_id).then(response => {
    form.value = response.data;
    form.value.summary = form.value.summary.replace(/\n/g, '<br/>')
    infoOpen.value = true;
    title.value = "工作报告";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDailyReports(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改工作报告";
  });
}



/** 提交按钮 */
function submitForm() {
  proxy.$refs["dailyReportsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDailyReports(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDailyReports(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除工作报告编号为"' + _ids + '"的数据项？').then(function() {
    return delDailyReports(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('media/dailyReports/export', {
    ...queryParams.value
  }, `dailyReports_${new Date().getTime()}.xlsx`)
}

getList();
</script>

